<?php

namespace XWAM\Addon\POrgCustomer\Model;

/**
 * CustomerModel类
 *
 * @since VER:1.0; DATE:2016-1-22; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
 */
class CustomerModel extends \XWAM\Model\AppbaseModel {

    protected $tableName = 't_porg_customer';

    /**
     * 获取客户
     * @param mixed $where
     * @return boolean
     * @since VER:1.0; DATE:2016-1-22; AUTHOR:SoChishun; EMAIL:14507247@qq.com; DESC:Added.
     */
    function find_customer($where) {
        $cache_key = 'find_customer_' . md5(serialize($where));
        $cache_data = S($cache_key);
        if ($cache_data) {
            return $cache_data;
        }

        $data = false;
        if ($where) {
            $data = $this->where($where)->find();
        }
        if ($data) {
            $this->where($where)->setField('review_time', date('Y-m-d h:i:s')); // 更新最后联系时间
        } else {
            $serial_no = sofn_generate_serial('KH' . $this->user_login_data['id']);
            $data = array('serial_no' => $serial_no, 'telphone' => $tel);
        }
        S($cache_key, $data, 15);
        return $data;
    }

    // 保存客户 2016-1-22
    function save_customer() {
        $rules = array(
            array('serial_no', 'require', '客户编号无效!', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
            array('name', 'require', '客户姓名无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
            array('telphone', 'require', '联系电话无效!', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        );
        if (!$this->validate($rules)->create()) {
            return $this->returnMsg(false, $this->error);
        }
        //图片上传
        //$msg = \Common\Controller\UploadHandlerController::upload(array('savePath' => 'Customer/', 'skipEmpty' => true));
        //if (is_array($msg) && isset($msg['face_url'])) {
        //    $this->data['face_url'] = $msg['face_url']['filepath'];
        //}
        $address_data = array(
            'name' => $this->data['name'],
            'telphone' => $this->data['telphone'],
            'address' => $this->data['address'],
            'zip' => $this->data['zip'],
        );
        $id = false;
        if (empty($this->id)) {
            $this->status = 1;
            $result = $this->add();
            $id = $result;
        } else {
            $id = $this->id;
            $result = $this->save();
        }
        if (false !== $result && $id) {
            AddonD('CustomerAddress')->add_customer_address($address_data, $id);
        }
        return $this->returnMsg($result);
    }

    // 物理删除客户
    function delete_customer($id) {
        $result = $this->delete($id);
        return $this->returnMsg($result);
    }

    // 逻辑删除客户
    function recycle_customer($id) {
        $result = $this->where(array('id' => array('in', $id)))->setField('status', 4);
        return $this->returnMsg($result);
    }

    // 逻辑恢复客户
    function restore_customer($id) {
        $result = $this->where(array('id' => array('in', $id)))->setField('status', 1);
        return $this->returnMsg($result);
    }

}
